package cn.itcast.bos.dao.staff;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Repository;

import cn.itcast.bos.dao.base.BaseDaoImpl;
import cn.itcast.bos.domain.PageBean;
import cn.itcast.bos.domain.Staff;

@Repository
public class StaffDaoImpl extends BaseDaoImpl<Staff> implements IStaffDao {

	@Override
	public void pageQuery(PageBean pageBean) {
		int currentPage = pageBean.getCurrentPage();
		int pageSize = pageBean.getPageSize();
		DetachedCriteria detachedCriteria = pageBean.getDetachedCriteria();
		
		detachedCriteria.setProjection(Projections.rowCount());
		List<Long> list = this.getHibernateTemplate().findByCriteria(detachedCriteria);
		Long total = list.get(0);
		
		//查询当前页显示的数据集合
		detachedCriteria.setProjection(null);
		//指定hibernate框架从数据表中查询的每行对应的数据对应的包装成一个实体对象
		detachedCriteria.setResultTransformer(DetachedCriteria.ROOT_ENTITY);
		
		int firstResults = (currentPage-1)*pageSize;
		int maxResults = pageSize;
		List recordList = this.getHibernateTemplate().findByCriteria(detachedCriteria, firstResults, maxResults);
		pageBean.setTotal(total.intValue());
		pageBean.setRows(recordList);
	}

}
